package com.xuexiang.yoyo.sql.mapper;

import android.content.ContentValues;
import android.database.Cursor;

import androidx.annotation.NonNull;

import com.xuexiang.yoyo.constant.HesAppStatic;
import com.xuexiang.yoyo.entity.ProfileGenericObject;

import java.util.ArrayList;
import java.util.List;

public class ProfileGenericObjectMapper {
    public static final String TABLE = "profile_generic_object";
    public static final String CREATE_TABLE_SQL="CREATE TABLE "+ TABLE+"(id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT," +
            "data_item_name TEXT," +
            "ln TEXT," +
            "scaler DOUBLE," +
            "capture_period integer," +
            "unit TEXT," +
            "i integer," +
            "username TEXT," +
            "parent_ln TEXT" +
            ",device_code TEXT," +
            "fcat_tag TEXT);";

    public static List<ProfileGenericObject> queryByDeviceCode(String deviceCode, String ln){
        Cursor cursor = HesAppStatic.getmDataBaseOpenHelper().query(TABLE, "where device_code='" + deviceCode + "'" +
                " and username='" + HesAppStatic.getUsername() + "' " +
                " and parent_ln='"+ln+"'");
        List<ProfileGenericObject> resultList = getProfileGenericObjects(cursor);
        cursor.close();
        return resultList;
    }

    @NonNull
    private static List<ProfileGenericObject> getProfileGenericObjects(Cursor cursor) {
        List<ProfileGenericObject> resultList = new ArrayList<>();
        while (cursor.moveToNext()) {
            ProfileGenericObject item = new ProfileGenericObject();
            item.setId(cursor.getLong(0));
            item.setDataItemName(cursor.getString(1));
            item.setLn(cursor.getString(2));
            item.setScaler(cursor.getDouble(3));
            item.setCapturePeriod(cursor.getInt(4));
            item.setUnit(cursor.getString(5));
            item.setI(cursor.getInt(6));
            item.setUsername(cursor.getString(7));
            item.setParentLn(cursor.getString(8));
            item.setDeviceCode(cursor.getString(9));
            item.setfcatTag(cursor.getString(10));
            resultList.add(item);

        }
        return resultList;
    }
    public static void delete(String device_code,String parentLn){
        HesAppStatic.getmDataBaseOpenHelper().delete(TABLE,"username='" + HesAppStatic.getUsername() + "' and device_code= '"+device_code+"' "+ "  and parent_ln= '"+parentLn+"' ",null);
    }
    public static void delete(String device_code){
        HesAppStatic.getmDataBaseOpenHelper().delete(TABLE,"username='" + HesAppStatic.getUsername() + "' and device_code= '"+device_code+"'",null);
    }
    public static void delete(){
        HesAppStatic.getmDataBaseOpenHelper().delete(TABLE,"username='" + HesAppStatic.getUsername() +"'",null);
    }
    public static void insert(ProfileGenericObject item){
        ContentValues contentValues = new ContentValues();
        contentValues.put("data_item_name", item.getDataItemName());
        contentValues.put("ln", item.getLn());
        contentValues.put("scaler", item.getScaler());
        contentValues.put("capture_period", item.getCapturePeriod());
        contentValues.put("unit", item.getUnit());
        contentValues.put("i", item.getI());
        contentValues.put("username", HesAppStatic.getUsername());
        contentValues.put("parent_ln", item.getParentLn());
        contentValues.put("device_code", item.getDeviceCode());
        contentValues.put("fcat_tag", item.getfcatTag());
        HesAppStatic.getmDataBaseOpenHelper().insert(TABLE,contentValues);
    }
    public static List<ProfileGenericObject> queryByDeviceCode(String deviceCode){
        Cursor cursor = HesAppStatic.getmDataBaseOpenHelper().query(TABLE, "where device_code='" + deviceCode + "'" +
                " and username='" + HesAppStatic.getUsername() + "'");
        List<ProfileGenericObject> resultList = getProfileGenericObjects(cursor);
        cursor.close();
        return resultList;
    }

    public static List<ProfileGenericObject> queryByDeviceCodeAndDataItemName(String deviceCode, String dataItemName){
        Cursor cursor = HesAppStatic.getmDataBaseOpenHelper().query(TABLE, "where device_code='" + deviceCode + "'" +
                " and username='" + HesAppStatic.getUsername() + "'" +
                " and data_item_name='" + dataItemName + "'" );
        List<ProfileGenericObject> resultList = getProfileGenericObjects(cursor);
        cursor.close();
        return resultList;
    }



    public static List<ProfileGenericObject> queryByUsername(){
        Cursor cursor = HesAppStatic.getmDataBaseOpenHelper().query(TABLE, "where  username='" + HesAppStatic.getUsername() + "'");
        List<ProfileGenericObject> resultList = getProfileGenericObjects(cursor);
        cursor.close();
        return resultList;
    }

}
